#!/bin/sh
#
# TODO:
#  - test bextract
#  - with strip path
#
# Run a accurate backup 
#   then restore it.
#

TestName="accurate-test"
JobName=backup
. scripts/functions

p() {
   echo "##############################################" >> ${cwd}/tmp/log1.out
   echo "$*" >> ${cwd}/tmp/log1.out
   echo "##############################################" >> ${cwd}/tmp/log2.out
   echo "$*" >> ${cwd}/tmp/log2.out
   if is_debug; then
      echo "##############################################"
      echo "$*"
   fi
}

# Directory to backup.
# This directory will be created by setup_data().
# If set, $BackupDirectory will be used
# as base directory by check_restore_diff().
# Don't use a "/" at the end of the directory path,
# as it might cause problems
# (filenames given as parameter to restore must match 100%. "//" are not identical with "/").
BackupDirectory="${tmp}/data"

# Remove old configuration, working and tmp files. Setup the database.
cleanup

# Use a tgz to setup data to be backed up.
# Data will be placed at "${tmp}/data".
setup_data data/small.tgz

copy_test_confs
cp -f $rscripts/susan-dir.conf.accurate $conf/susan-dir.conf
sed s/all,/all,saved,/ $conf/susan-fd.conf > tmp/1
cp tmp/1 $conf/susan-fd.conf

change_jobname BackupClient1 $JobName

# add extra files
mkdir -p ${BackupDirectory}/build/accurate
mkdir -p ${BackupDirectory}/build/accurate/dirtest
echo "test test" > ${BackupDirectory}/build/accurate/dirtest/hello
echo "test test" > ${BackupDirectory}/build/accurate/xxx
echo "test test" > ${BackupDirectory}/build/accurate/yyy
echo "test test" > ${BackupDirectory}/build/accurate/zzz
echo "test test" > ${BackupDirectory}/build/accurate/zzzzzz
echo "test test" > ${BackupDirectory}/build/accurate/xxxxxx
echo "test test" > ${BackupDirectory}/build/accurate/yyyyyy
echo "test test" > ${BackupDirectory}/build/accurate/xxxxxxxxx
echo "test test" > ${BackupDirectory}/build/accurate/yyyyyyyyy
echo "test test" > ${BackupDirectory}/build/accurate/zzzzzzzzz
echo ${BackupDirectory} > ${cwd}/tmp/file-list

start_test

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
label volume=TestVolume001 storage=File pool=Default
messages
END_OF_DATA

run_susan

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log1.out
run job=$JobName yes
wait
messages
@#
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
setdebug level=10 storage=File
restore fileset=FS_TESTJOB where=${cwd}/tmp/susan-restores select all done
yes
wait
messages
quit
END_OF_DATA

################################################################
p First :  We just run full and restore to compare if all is ok
################################################################

run_bconsole
check_for_zombie_jobs storage=File

check_two_logs
check_restore_diff

rm -rf ${cwd}/tmp/susan-restores

################################################################
p Now do a second backup after making few changes
################################################################
rm ${BackupDirectory}/build/accurate/xxx  # delete a file
rm ${BackupDirectory}/build/accurate/dirtest/hello

run_bconsole
check_for_zombie_jobs storage=File

check_two_logs
check_restore_diff
check_files_written ${cwd}/tmp/log1.out 4

rm -rf ${cwd}/tmp/susan-restores

################################################################
p Now do a third backup after making few changes
################################################################
rm ${BackupDirectory}/build/accurate/yyyyyy  # delete a file
rmdir ${BackupDirectory}/build/accurate/dirtest

run_bconsole
check_for_zombie_jobs storage=File

check_two_logs
check_restore_diff
check_files_written ${cwd}/tmp/log1.out 3

rm -rf ${cwd}/tmp/susan-restores

################################################################
p "Now do a 4 backup after making few changes"
################################################################
rm ${BackupDirectory}/build/accurate/zzzzzz  # delete a file

run_bconsole
check_for_zombie_jobs storage=File

check_two_logs
check_restore_diff
check_files_written ${cwd}/tmp/log1.out 2

rm -rf ${cwd}/tmp/susan-restores

################################################################
p Now do a 5 backup after making few changes
################################################################
rm ${BackupDirectory}/build/accurate/zzzzzzzzz

run_bconsole
check_for_zombie_jobs storage=File

check_two_logs
check_restore_diff
check_files_written ${cwd}/tmp/log1.out 2

rm -rf ${cwd}/tmp/susan-restores

################################################################
p "Now do a backup after making few changes"
################################################################
touch ${BackupDirectory}/build/accurate/aaaaaa

run_bconsole
check_for_zombie_jobs storage=File

check_two_logs
check_restore_diff
check_files_written ${cwd}/tmp/log1.out 2

rm -rf ${cwd}/tmp/susan-restores

################################################################
p Check with bls
################################################################

$bin/bls -c $conf -V 'TestVolume001' FileStorage > $tmp/bls.out
grep -- '----' $tmp/bls.out | grep xxx > /dev/null
if [ $? != 0 ] ; then
    print_debug "ERROR: Should find deleted files into $tmp/bls.out"
    bstat=2
fi

################################################################
p "Now do a backup after making few changes"
################################################################

# some files will have disappear, others have their old mtime/ctime
mv ${BackupDirectory}/build/accurate ${BackupDirectory}/build/accurate.new

run_bconsole
check_for_zombie_jobs storage=File

check_two_logs
check_restore_diff

rm -rf ${cwd}/tmp/susan-restores

################################################################
p Now do an other test in differential mode
################################################################

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log1.out
run job=$JobName level=differential yes
wait
messages
@#
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
setdebug level=10 storage=File
restore fileset=FS_TESTJOB where=${cwd}/tmp/susan-restores select all done
yes
wait
messages
quit
END_OF_DATA

run_bconsole
check_for_zombie_jobs storage=File

check_two_logs
check_restore_diff

rm -rf ${cwd}/tmp/susan-restores

################################################################
p "Now do an other test in differential mode + incremental"
################################################################

# make some changes
mv ${BackupDirectory}/build/accurate.new ${BackupDirectory}/build/accurate

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log1.out
run job=$JobName yes
wait
messages
@#
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
setdebug level=10 storage=File
restore fileset=FS_TESTJOB where=${cwd}/tmp/susan-restores select all done
yes
wait
messages
quit
END_OF_DATA

run_bconsole
check_for_zombie_jobs storage=File

check_two_logs
check_restore_diff

rm -rf ${cwd}/tmp/susan-restores

################################################################
p "Now do a backup after making few changes"
################################################################
rm ${BackupDirectory}/build/accurate/aaaaaa
touch ${BackupDirectory}/build/accurate/bbbbbb

run_bconsole
check_for_zombie_jobs storage=File
check_files_written ${cwd}/tmp/log1.out 3

check_two_logs
check_restore_diff

################################################################
p "Now do a backup after making few changes"
################################################################
mv ${cwd}/tmp/susan-restores ${BackupDirectory}/build/accurate/

run_bconsole
check_for_zombie_jobs storage=File

check_two_logs
check_restore_diff

rm -rf ${cwd}/tmp/susan-restores ${BackupDirectory}/build/accurate/susan-restores

#
#################################################################
#p Check with bscan
#################################################################
#
#stop_susan
#
#cd $bin
#  ./drop_susan_tables      >/dev/null 2>&1
#  ./make_susan_tables      >/dev/null 2>&1
#  ./grant_susan_privileges >/dev/null 2>&1
#cd ..
#
#echo "volume=TestVolume001" >tmp/bscan.bsr
#
#bscan_libdbi
#
#$bin/bscan -c $conf/susan-sd.conf $BSCANLIBDBI -n "$db_name" -u "$db_user" -m -s -b $tmp/bscan.bsr FileStorage 2>&1 > $tmp/bscan.log
#
#cat <<END_OF_DATA >${cwd}/tmp/bconcmds
#@$out ${cwd}/tmp/log1.out
#messages
#@#
#@# now do a restore after bscan
#@#
#@$out ${cwd}/tmp/log2.out
#setdebug level=10 storage=File
#restore fileset=FS_TESTJOB where=${cwd}/tmp/susan-restores select all done
#yes
#wait
#messages
#quit
#END_OF_DATA
#
# run susan with just the restore job
run_susan

check_for_zombie_jobs storage=File

check_two_logs
check_restore_diff

rm -rf ${cwd}/tmp/susan-restores

################################################################
p "Now do a test with other attributes: owner, gid, rights"
################################################################

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log1.out
label volume=TestVolume002 storage=File pool=Default
run job=backup_advance yes
wait
messages
@#
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
setdebug level=10 storage=File
restore fileset=FS_TESTJOB_ADVANCE where=${cwd}/tmp/susan-restores select all done
yes
wait
messages
quit
END_OF_DATA

run_bconsole
check_for_zombie_jobs storage=File

check_two_logs
check_restore_diff

rm -rf ${cwd}/tmp/susan-restores


################################################################
p Use the p option for verify
################################################################

chmod 400 ${BackupDirectory}/build/accurate/yyy

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log1.out
run job=backup_advance yes
wait
messages
@#
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
setdebug level=10 storage=File
restore fileset=FS_TESTJOB_ADVANCE where=${cwd}/tmp/susan-restores select all done
yes
wait
messages
quit
END_OF_DATA

run_bconsole
check_for_zombie_jobs storage=File

check_two_logs
check_restore_diff
check_files_written ${cwd}/tmp/log1.out 1

rm -rf ${cwd}/tmp/susan-restores

################################################################
p "Test strippath option"
################################################################

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log1.out
setdebug  level=1 client=$CLIENT
run job=backup fileset=FS_TESTJOB2 yes
wait
messages
@$out ${cwd}/tmp/log3.out
st dir
quit
END_OF_DATA

run_bconsole
check_for_zombie_jobs storage=File

# run incremental
rm -f ${BackupDirectory}/build/accurate/yyy
run_bconsole
check_for_zombie_jobs storage=File

jobid=`awk '/ Incr.+backup/ { jobid=$1 } END { print jobid }' ${cwd}/tmp/log3.out`

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log3.out
list files jobid=$jobid
quit
END_OF_DATA

run_bconsole

grep yyy ${cwd}/tmp/log3.out > /dev/null
if [ $? != 0 ] ; then
    print_debug "ERROR: Can't find yyy file into 'list files' output (${cwd}/tmp/log3.out)"
    dstat=2
fi

grep zzz ${cwd}/tmp/log3.out > /dev/null
if [ $? = 0 ] ; then
    print_debug "ERROR: Should not find zzz file into 'list files' output (${cwd}/tmp/log3.out)"
    dstat=2
fi

stop_susan
end_test
